package com.sjz.han.industry.activites;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.dfqin.grantor.PermissionListener;
import com.github.dfqin.grantor.PermissionsUtil;
import com.sjz.han.industry.BuildConfig;
import com.sjz.han.industry.R;
import com.sjz.han.industry.base.BaseActivity;
import com.sjz.han.industry.entities.KeFuJsEntity;
import com.sjz.han.industry.entities.LoignEntity;
import com.sjz.han.industry.internetmanage.ReqestUrl;
import com.sjz.han.industry.utils.JsonUtils;
import com.sjz.han.industry.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

//客服中心
public class KeFuActivity extends BaseActivity implements View.OnClickListener{
    private TextView kefu_phone_tv;
    private EditText liuyan_edt;
    private Button liuyan_btn;
    @Override
    protected int setLayoutId() {
        return R.layout.activity_kefu;
    }

    @Override
    protected void initDatas() {

    }

    @Override
    protected void initViews() {
        reback_btn=(RelativeLayout)this.findViewById(R.id.reback_btn);
        titleTv=(TextView)this.findViewById(R.id.ctitle_tv);
        titleTv.setText("客服中心");

        kefu_phone_tv=(TextView)this.findViewById(R.id.kefu_phone_tv);
        liuyan_edt=(EditText)this.findViewById(R.id.liuyan_edt);
        liuyan_btn=(Button)this.findViewById(R.id.liuyan_btn);
    }

    @Override
    protected void setLisener() {
        reback_btn.setOnClickListener(this);
        kefu_phone_tv.setOnClickListener(this);
        liuyan_btn.setOnClickListener(this);
    }

    @Override
    protected void onRetry() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.reback_btn:
                finish();
                break;
            case R.id.kefu_phone_tv://拨打电话

                PermissionsUtil.requestPermission(this, new PermissionListener() {
                    @Override
                    public void permissionGranted(@NonNull String[] permission) {
//                            Intent intent = new Intent(Intent.ACTION_CALL);
                            Intent intent = new Intent(Intent.ACTION_DIAL);
                            Uri data = Uri.parse("tel:4000-8000-6000" );
                            intent.setData(data);
                            startActivity(intent);

                    }

                    @Override
                    public void permissionDenied(@NonNull String[] permission) {
                        ToastUtils.popUpToast("请同意电话权限");
                    }
                }, new String[]{
                        Manifest.permission.CALL_PHONE});
                break;
            case R.id.liuyan_btn://留言阿牛
                if(TextUtils.isEmpty(liuyan_edt.getText().toString().trim())){
                 ToastUtils.popUpToast("留言不可为空！");
                }else{
                  uploadLiuYan(liuyan_edt.getText().toString().trim());
                }
                break;
        }
    }

    public void uploadLiuYan(String content){
        showLoadingDialog("正在提交...");
        OkHttpUtils
                .post()
                .url(ReqestUrl.TJ_LY_URL)
                .addParams("token",shareUtils.getToken())
                .addParams("content",content)
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.popUpToast("请求超时");
            }
            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if(ReqestUrl.rebacRequestJson(response)!=null){
                    LoignEntity entity= JsonUtils.getObject(response,LoignEntity.class);
                    if(entity!=null&&entity.code==0){
                        liuyan_edt.setText("");
                        ToastUtils.popUpToast(entity.msg);

                    }else{
                        ToastUtils.popUpToast(entity.msg);
                    }
                }
            }
        });
    }
}
